//---------------------------------------------------------------------------

#ifndef h_portalH
#define h_portalH
//---------------------------------------------------------------------------
class HBatchIcon;

struct HBatchOutline
{
   UTF8String Name;
   UTF8String Caption;
   UTF8String IpAddr;
   int Port;
   UTF8String HotbackIpAddr;
   int HotbackPort;
   bool HotbackFlag;
   UTF8String Protocol;
   bool EnableRestart;
   bool ShowBatchDate;
   bool Offline;
   UTF8String StartWindow;
   UTF8String FadingPeriod;
   UTF8String BatchInterval;
   UTF8String JobNameDelimiter;
   UTF8String Description;

   __fastcall HBatchOutline()
      : Port(0), HotbackPort(0), HotbackFlag(false), EnableRestart(false),
        ShowBatchDate(true), Offline(false)
   {}
};

struct HOutlineStatus
{
   bool AutoSkip;
   UTF8String Banner;

   bool AppendLogToSummary;
   int ParaJobNum;
   UTF8String CodeSet;
   UTF8String DailyBatch;
   UTF8String BatchAlarm;
   bool Dedicated;
   UTF8String BatchDate;
   UTF8String NextBatchDate;
   UTF8String EndBatchDate;
   UTF8String NextScheduleDate;
   UTF8String Cluster;

   h_g::HIconType Status;
   UTF8String StartTime;
   UTF8String EndTime;
   UTF8String CompletedRatio;
   UTF8String JobName;

   UTF8String StatusStr;
   UTF8String AlarmType;
   UTF8String RefreshTick;
   bool FullRefresh;

   __fastcall HOutlineStatus()
      :  AutoSkip(false), ParaJobNum(0), Dedicated(false),
         Status(h_g::itUnknown), FullRefresh(false)
   {}
};

class HOutlineThread : public HThread
{
public :
   __fastcall HOutlineThread(bool CreateSuspended, HBatchIcon *Batch)
      : HThread(CreateSuspended), FBatch(Batch)
   {
      FRefreshEvent = new TSimpleEvent(false);
   }
   virtual __fastcall ~HOutlineThread()
   {
      delete FRefreshEvent;
   }
   virtual void __fastcall Execute();
   void __fastcall Refresh()
   {
      FRefreshEvent->SetEvent();
   }
private :
   TSimpleEvent *FRefreshEvent;
   HBatchIcon *FBatch;
};

class HBatchIcon : public TGraphicControl
{
public :
   friend HOutlineThread;
   __fastcall virtual HBatchIcon(Classes::TComponent* AOwner, int AIdx);
   __fastcall virtual ~HBatchIcon();
   void __fastcall StartThread()
   {
      Thread->Start();
   }
   void __fastcall Refresh()
   {
      FThread->Refresh();
   }
   void __fastcall AbortRefresh()
   {
      FAbortFlag = true;
      FThread->Refresh();
   }
   void __fastcall Lock()
   {
      Cs->Enter();
   }
   void __fastcall Unlock()
   {
      Cs->Leave();
   }
   void __fastcall WaitThread()
   {
      FThread->WaitFor();
   }

   __property HXmlTree *RefreshXml = { read = FRefreshXml, write = FRefreshXml };
   __property HBatchOutline Outline = { read = FOutline, write = FOutline };
   __property HOutlineStatus OutlineStatus = { read = FOutlineStatus,
      write = SetOutlineStatus };
   __property HOutlineStatus NewOutlineStatus = { read = FNewOutlineStatus,
      write = FNewOutlineStatus };
   __property HOutlineThread *Thread = { read = FThread, write = FThread };
   __property int Idx = { read = FIdx, write = FIdx };
   __property bool Entered = { read = FEntered, write = SetEntered };
   __property bool AbortFlag = { read = FAbortFlag,
      write = FAbortFlag };
   __property bool PaintingFlag = { read = FPaintingFlag,
      write = FPaintingFlag };
   __property UTF8String LastStartTime = { read = FLastStartTime,
      write = FLastStartTime };
   __property UTF8String LastRefreshTick = { read = FLastRefreshTick,
      write = FLastRefreshTick };
   __property char *DecompressBuf = { read = FDecompressBuf,
      write = FDecompressBuf };
   __property UTF8String StatusFileName = { read = FStatusFileName,
      write = FStatusFileName };

   UTF8String LastLoginId;
   HNavInfo NavInfo;

protected :
   virtual void __fastcall Paint(void);

   struct HAlarmJobInfo
   {
      UTF8String Name;
      int Hour;
      int Min;
      __fastcall HAlarmJobInfo()
         : Hour(-1), Min(-1)
      {}
   };

private :

   HOutlineStatus __fastcall GetNewOutlineStatus();
   void __fastcall SetOutlineStatus(const HOutlineStatus &OutlineStatus);
   void __fastcall SetEntered(bool AEntered);

   TCriticalSection *Cs;
   HOutlineThread *FThread;
   HXmlTree *FRefreshXml;
   HBatchOutline FOutline;
   HOutlineStatus FOutlineStatus;
   HOutlineStatus FNewOutlineStatus;
   UTF8String FLastStartTime;
   UTF8String FLastRefreshTick;
   int FIdx;
   bool FEntered;
   bool FAbortFlag;
   bool FPaintingFlag;
   char *FDecompressBuf;
   UTF8String FStatusFileName;
};

class HPortal : public TScrollingWinControl
{
public :

        typedef TScrollingWinControl inherited;
        __property PopupMenu;
        __property int ColQty = { read = FColQty, write = FColQty };
        __fastcall virtual HPortal(Classes::TComponent* AOwner);
        __fastcall virtual ~HPortal();
        void __fastcall Init(int ColQty, HXmlTree *Cfg, const UTF8String &GotoIp,
           int GotoPort, const UTF8String &GotoGroup);
        void __fastcall RefreshCaption();
        void __fastcall Refresh();
        void __fastcall Close();
        void __fastcall ShowBatchProperties();
        void __fastcall SetCurrentBatch(HBatchIcon *Batch);
        void __fastcall SetCurrentBatch(int Idx);
        HBatchIcon * __fastcall FindByCaption(const UTF8String &Caption);
        int __fastcall GetBatchQty()
        {
           return IconVec.size();
        }
        HBatchIcon * __fastcall GetFirstBatch();
        void __fastcall GetAllBatch(vector<HBatchIcon*> &BatchVec)
        {
           BatchVec = IconVec;
        }

        static void __fastcall GetAlarmTime(const char *TimeStr, int &Hour, int &Min);

        void __fastcall SaveToBatList(map<int, HBatListInfo> &BatMap);

        HBatchIcon *CurrentBatch;
private :

        TControlCanvas *Canvas;
        int FColQty;
        vector<HBatchIcon*> IconVec;
        map<UTF8String, HBatchIcon*> IconMap;

        void __fastcall WMPaint(TWMPaint &Message);
        void __fastcall WMLButtonDown(TWMLButtonDown &Message);
        void __fastcall WMLButtonDblClk(TWMLButtonDblClk &Message);
        void __fastcall WMRButtonUp(TWMRButtonUp &Message);
        HBatchIcon * __fastcall GetBatchByPos(int XPos, int YPos);
        void __fastcall InvalidateCurrentBatchRect();
protected :

        void __fastcall WndProc(TMessage &Message);
        BEGIN_MESSAGE_MAP
           VCL_MESSAGE_HANDLER(WM_PAINT, TWMPaint,
              WMPaint);
           VCL_MESSAGE_HANDLER(WM_LBUTTONDOWN, TWMLButtonDown, WMLButtonDown);
           VCL_MESSAGE_HANDLER(WM_LBUTTONDBLCLK, TWMLButtonDblClk,
              WMLButtonDblClk);
           VCL_MESSAGE_HANDLER(WM_RBUTTONUP, TWMRButtonUp,
              WMRButtonUp);
        END_MESSAGE_MAP(TScrollingWinControl);
};

class HLogoIcon : public TGraphicControl
{
public :
   __fastcall virtual HLogoIcon(System::Classes::TComponent* AOwner);
   __fastcall virtual ~HLogoIcon();
protected :
   virtual void __fastcall Paint(void);
};

#endif
